package com.contactbook.web.client;

import com.contactbook.web.client.dto.GroupDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CreateGroupWidget extends View {

	private VerticalPanel content;
	
	private final static String NAME_TEXTBOX_SIZE = "100px";
	private final static String DESC_TEXTAREA_WSIZE = "200px";
	private final static String DESC_TEXTAREA_HSIZE = "90px";
	
	private final static String TITLE = "Create a new group of contacts";
	
	private final TextBox nameTb;
	private final TextArea textAreaDescription;
	
	public CreateGroupWidget(ContactBook contactBook) {
		super(contactBook);
		
		nameTb = new TextBox();
		textAreaDescription = new TextArea();
	}
	
	@Override
	public VerticalPanel getContent() {
		if (content == null)
			return buildView();
		return content;
	}
	
	public VerticalPanel buildView() {
		
		content = new VerticalPanel();
		
		final Label labelNote = new Label("Enter the information of the group you want to create. You must give at least a name.");
		labelNote.setStyleName("gwt-label-note");
		
		content.add(labelNote);
		
		final FlexTable table = new FlexTable();
		table.setTitle("");
		
		final Label labelName = new Label();
		labelName.setText("Name :");
		table.setWidget(0, 0, labelName);
		
		nameTb.setTitle("Name of the group");
		nameTb.setWidth(NAME_TEXTBOX_SIZE);
		table.setWidget(0, 1, nameTb);
		
		final Label labelDescription = new Label();
		labelDescription.setText("Description :");
		table.setWidget(1, 0, labelDescription);
		
		textAreaDescription.setTitle("Your description");
		textAreaDescription.setWidth(DESC_TEXTAREA_WSIZE);
		textAreaDescription.setHeight(DESC_TEXTAREA_HSIZE);
		table.setWidget(1, 1, textAreaDescription);
		
		final Button buttonOk = new Button("Ok");
		buttonOk.addClickHandler(new OkHandler());
		final Button buttonCancel = new Button("Cancel");
		buttonCancel.addClickHandler(new CancelHandler());
		
		table.setWidget(2, 0, buttonOk);
		table.setWidget(2, 1, buttonCancel);

		content.add(table);
		
		return content;
	}
	
	private class CancelHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			contactBook.switchView("listOfContacts");
		}
	}
	
	private class OkHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			String name = nameTb.getText();
			String description = textAreaDescription.getText();
			if (!name.isEmpty()) {
				GroupDTO groupDTO = new GroupDTO();
				groupDTO.setAccount(contactBook.getCurrentSession().getAccountLogged());
				groupDTO.setName(name);
				groupDTO.setDescription(description);
				AsyncCallback<GroupDTO> callback = new AsyncCallback<GroupDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						new DialogBoxWidget(contactBook, "error");
					}

					@Override
					public void onSuccess(GroupDTO result) {
						contactBook.switchView(Constants.ListOfContactsViewName);
					}
				};
				contactBook.getGroupService().add(groupDTO, contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
			}
			else {
				new DialogBoxWidget(contactBook, "You must give at least the name of the new group.");
			}
		}
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
}
